import java.util.List;
import java.util.Stack;

/**
 * @Author 12629
 * @Description：
 */
public class MySingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//永远指向我的头节点

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    //遍历单链表
    public void show() {
        //这里不是定义了一个节点 这里只是一个引用
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void show(ListNode newHead) {
        //这里不是定义了一个节点 这里只是一个引用
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度 --》 链表中节点的个数
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null) {
            //如果val值 是引用类型  那么这里得用equals来进行比较！！！
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur 指向的节点就是尾巴节点
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        int len = size();
        //0、判断index位置的合法性
        if(index < 0 || index > len) {
            throw new IndexOutOfBounds("任意位置插入数据的时候，index位置不合法: "+index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        //1、先找到index-1位置的节点
        ListNode cur = findIndex(index);
        //2、进行插入
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index-1位置的节点
     * @param index
     * @return
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;//index-1位置的节点
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if(prev == null) {
            System.out.println("没有这个数据！");
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;
    }

    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while (prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val ==  key) {
            head = head.next;
        }
    }
    public void clear() {
        //this.head = null;
        while (head != null) {
            ListNode headNext = head.next;
            head.next = null;
            head = headNext;
        }
    }

    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            //记录 当前需要翻转节点的下一个节点
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }


    public ListNode middleNode(ListNode head) {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode findKthToTail(int k) {
        // 一会儿我会省略掉这个size()
        if(k <= 0 || head == null) {
            return  null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1、先让fast走k-1步
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if(fast == null) {
                return null;
            }
        }
        //2、同时走 走到fast.next==null的时候停止！ 返回slow
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode partition( int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while (cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;
                    //cur = cur.next;省略1
                }else {
                    be.next = cur;
                    be = cur;
                    //cur = cur.next;省略1
                }
                //cur = cur.next; 省略2
            }else {
                //第一次插入的时候
                if(as == null) {
                    as = cur;
                    ae = cur;
                    //cur = cur.next;省略1
                }else {
                    ae.next = cur;
                    ae = cur;
                    //cur = cur.next;省略1
                }
                //cur = cur.next;省略2
            }
            cur = cur.next;
        }
        //要考虑： 难道2个段当中 都有数据吗？
        // 第一个段没有数据 直接返回第2个段！！
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        // write code here
        if(head == null) return false;
        //1、找中间节点
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //slow所指的位置就是中间节点
        //2、开始翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //此时翻转完成
        //3、开始判断是否为回文
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            //针对偶数节点
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }

    public ListNode detectCycle(ListNode head) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public void show3(ListNode head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.println(head.val);
            return;
        }
        show3(head.next);
        System.out.println(head.val);
    }

    public void show4() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        //依次出栈
        while (!stack.empty()) {
            ListNode tmp = stack.pop();
            System.out.println(tmp.val);
        }
    }
}




